<?php

use Symfony\Component\Translation\Interval;

class CommonHelper {
	
	/**
	 * Detect controller to active link
	 * @author Duc Nguyen
	 */
	public static function activeLink($controller, $admin = false) {
		if (!empty($admin)) {
			if (Request::segment(1) == 'admin' && Request::segment(2) == $controller) {
				return 'start active open';
			}
		} else {
			if (Request::segment(1) == $controller) {
				return 'start active open';
			}
		}
		return '';
	}
	
	
	/**
	 * Generate String time
	 * @param: $minutes_total
	 * @return: string format: x hour y minutes
	 * @author Duc Nguyen
	 */
	public static function generate_string_time($minutes_total) {
		$num_hours = intval($minutes_total / 60);
		$num_minutes = $minutes_total - $num_hours*60;
		$string_format = '';
		if ($num_hours <= 0) {
			$num_hours = '00';
		} else if ($num_hours < 10) {
			$num_hours = "0".$num_hours;
		}
		if ($num_minutes <= 0) {
			$num_minutes = '00';
		}
		$string_format .= $num_hours . trans('form.label.hour_unit');
		$string_format .= $num_minutes . trans('form.label.minute_unit');
		return $string_format;
	}
	
	/**
	 * Convert string time to minutes
	 * @param $string_time
	 * @return $total minutes
	 * @author Duc Nguyen
	 */
	public static function convertToMinutes($string_time) {
		$totalMinutes = 0;
		$elements = explode(':', $string_time);
		if (count($elements) > 1) {
			$totalMinutes = intval($elements[0])*60 + intval($elements[1]);
		}
		return $totalMinutes;
	}
	
	/**
	 * List users
	 * @value group: 5
	 * @return: array users
	 * @author Duc Nguyen
	 */
	public static function getUsers() {
		$users = DB::table('users')	->join('users_groups', 'users.id', '=', 'users_groups.user_id')
									->whereRaw('users.is_deleted = ? and users.activated = ? and users_groups.group_id = ? and users.email != ?',
										array(0, 1, 5, ''))
									->distinct()
									->lists('email', 'id');
		return $users;
	}
	/**
	 * List managers
	 * @value group: 3
	 * @return: array managers
	 * @author Duc Nguyen
	 */
	public static function getManagers() {
		$managers = DB::table('users')	->join('users_groups', 'users.id', '=', 'users_groups.user_id')
									->whereRaw('users.is_deleted = ? and users.activated = ? and users_groups.group_id = ?and users.email != ?',
										array(0, 1, 3, ''))
									->distinct()
									->lists('email', 'id');
		return $managers;
	}
	
	
	/**
	 * Show price with en format or vi format
	 * EN format: ($)
	 * VI format: (tỉ VND)
	 * @param $price float (VND)
	 * @return float
	 * @author Duc Nguyen
	 */
	public static function showPrice($price, $language_code = 'en') {
		return $price;
	}
	
	/**
	 * Caculate price
	 * EN format: ($)
	 * VI format: (tỉ VND)
	 * @param $price float (VND)
	 * @return float VND
	 * @author Duc Nguyen
	 */
	public static function caculatePrice($price, $language_code = 'en') {
		if ($language_code == 'en') {
			$rate = 20800; // percent 1 $
		}
		if ($language_code == 'vi') {
			$rate = 1000000000; // 1 bilion
		}
		return round($price * $rate, 2);
	}
	
	
	/**
	 * ===================Get Group for User===============================
	 */
	 
	/**
     * Retrieve collection of groups that are not superior than the current user's one
     * @author Trung Vuong
     * @return mixed Collection or array
     */
    public static function nonSuperiorGroups() {
        $user = Sentry::getUser();
        if (!empty($user)) {
            $groups = $user->getGroups();
            if (!$groups->isEmpty()) {
                return Group::where('id', '>=', $groups[0]->getId())->get();
            }
        }
        return array();
    }
    
    /**
     * Generate array of groups as select options
     * @author Trung Vuong
     * @param mixed $groups null or Collection
     * @return array
     */
    public static function options($groups = null) {
        if (!is_object($groups)) {
            $groups = Sentry::findAllGroups();
        }
        
        $options = array();
        foreach ($groups as $group) {
            $options[ $group->getId() ] = Lang::get("group.name.{$group->getName()}");
        }
        return $options;
    }
    
      /**
     * get Role name by id
     * @author Thuan Truong
     * @param id
     * @return string
     */
    public static function getRoleName($id) {
        $role = Group::find($id);
        if ($role)
            return $role->name;
        return 0;
    }

	
	/**
	 * =================== Permission======================================= 
	 */
	
	
	/**
	 * Check role as admin
	 * @author Duc Nguyen
	 */
	public static function isAdmin() {
		if(Sentry::check()) {
			$user = Sentry::getUser();
			$adminGroup = Sentry::findGroupByName('admin');
			if($user->inGroup($adminGroup)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Check role as manager
	 * @author Duc Nguyen
	 */
	public static function isManager() {
		if(Sentry::check()) {
			$user = Sentry::getUser();
			$managerGroup = Sentry::findGroupByName('manager');
			if($user->inGroup($managerGroup)) {
				return true;
			}
		}
		return false;
	}
    
	/**
	 * Check role as user
	 * @author Duc Nguyen
	 */
	public static function isUser() {
		if(Sentry::check()) {
			$user = Sentry::getUser();
			$userGroup = Sentry::findGroupByName('user');
			if($user->inGroup($userGroup)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Check minimum role as manager
	 * @author Duc Nguyen
	 */
	public static function isMinManager() {
		if(CommonHelper::isManager()|| CommonHelper::isAdmin()) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * Check minimum role as user
	 * @author Duc Nguyen
	 */
	public static function isMinUser() {
		if(CommonHelper::isUser() || CommonHelper::isManager() || CommonHelper::isAdmin()) {
			return true;
		}
		return false;
	}
    
    /**
     * Check user is active
     * @author Thuan Truong
     */
    public static function isActived() {
        return BaseController::isActive();
    }
    
	public static function parentOptions() {
        $options = array(
            '1' => 'Nữ',
            '2' => 'Nam',
            '3' => 'Trẻ em',
            '4' => 'Quần áo & túi xách',
        );
        return $options;
    }
}